//
//  ClubInfoListViewController.swift
//  77Club
//
//  Created by Apple on 2024/8/27.
//

import UIKit
import JXSegmentedView

class ClubInfoListViewController: ClubBaseViewController {

    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.rowHeight = 102.0.Scale
        tableView.register(ClubInfoCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        return tableView
    }()
    var cateId = ""{
        didSet{
            loadData()
        }
    }
    var pageIndex = 1
    
    private var dataSource: [ClubHomeInfoItemModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            tableView
        }
        tableView.fillContainer()
        
        tableView.refresh_loadMore {
            self.loadMoreData()
        }.refresh_pull {
            self.loadNewData()
        }
    }
    
    private func loadData(){
        
        Task{
            let models = await network.requestMore(.infolist(class_id: cateId, page: pageIndex,keyword: ""), type: ClubHomeInfoModel.self)
            tableView.refresh_end()
            if models.isSuccess, let model = models.model {
                self.dataSource = model.data
                tableView.reloadData()
            }
        }
    }
    
    @objc private func loadNewData(){
        pageIndex = 1
        loadData()
    }
    
    @objc private func loadMoreData(){
        pageIndex += 1
        
        Task{
            let models = await network.requestMore(.infolist(class_id: cateId, page: pageIndex,keyword: ""), type: ClubHomeInfoModel.self)
            tableView.refresh_end()
            if models.isSuccess, let model = models.model {
                if let data = model.data, data.count > 0 {
                    for mod in data {
                        self.dataSource?.append(mod)
                    }
                }else{
                    self.tableView.refresh_end(true)
                }
                tableView.reloadData()
            }
        }
    }
}

//MARK: - UITableViewDelegate, UITableViewDataSource
extension ClubInfoListViewController: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return dataSource?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubInfoCell", for: indexPath) as! ClubInfoCell
        cell.model = dataSource?[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
            
            let model = dataSource?[indexPath.row]
//            guard let infoID = model?.id else { return }
//            let dict = ["uid": uid, "infoId": infoID]
            let webView = ClubWebViewController()
            var urlStr = model?.webview_url ?? ""
            if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
                urlStr = urlStr + "&uid=\(uid)"
            }
            urlStr = urlStr + "&model=ios"
            webView.urlString = urlStr
            webView.titleName = "资讯详情"
            webView.ishiddenNav = true
//            webView.JSData = dict
            self.navigationController?.pushViewController(webView, animated: true)
        }else{
            
            self.navigationController?.pushViewController(ClubLoginViewController(), animated: true)
        }
    }
}

extension ClubInfoListViewController:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}
